<?php

/**
 * class Bootstrap
 *
 * Description for class Bootstrap
 *
 * @author:
*/
class Bootstrap extends Zend_Application_Bootstrap_Bootstrap {
	
	/**
	 * This is method __construct
	 *
	 * @param mixed $app This is a description
	 * @return mixed This is the return value description
	 *
	 */	
	public function __construct($app) {
		parent::__construct($app);
		$this->_initAutoload();
		$this->_initViewHelpers();
		$this->_initRouter();
	}
	
	/**
	 * Enter description here...
	 *
	 * @return unknown
	 */
	protected function _initAutoload() {
		$autoloader = new Zend_Application_Module_Autoloader(array('namespace' => '', 
			'basePath'  => APPLICATION_PATH));
		return $autoloader;
	}	
	
	/**
	 * *
	 *
	 * @return mixed This is the return value description
	 *
	 */	
	public function _initRouter()
	{
		$front = Zend_Controller_Front::getInstance();
		$router = $front->getRouter();
		
		$apis = array('user');
		foreach	($apis as $api) {	// route to index
			$route = new Zend_Controller_Router_Route_Regex(
				'^api/' . $api . '/(\d+)([^/]+)$',
				array(
					'controller'   => 'api-' . $api,
					'action'       => 'index'),
				array(
					'index' => 'action',
					)
				);
			$router->addRoute('api-endpoint-index' .  $api, $route);
		}
		
		foreach	($apis as $api) {	// route to action
			$route = new Zend_Controller_Router_Route_Regex(
				'^api/' . $api . '/(\d+)/([^/\?]+)',
				array(
					'controller'   => 'api-' . $api,
					'action'       => 'index'),
				array(
					1 => 'user_id',
					2 => 'action',					
					)
				);
			$router->addRoute('api-endpoint-index' .  $api, $route);
		}			
		
		$apis = array('oauth', 'math');		
		foreach	($apis as $api) {
			$route = new Zend_Controller_Router_Route_Regex(
				'^api/' . $api . '/([^/\?]+)',
				array(
					'controller'   => 'api-' . $api,
					'action'       => 'index'),
				array(
					1 => 'action',
					)
				);
			$router->addRoute('api-endpoint' .  $api, $route);
		}

		$route = new Zend_Controller_Router_Route_Static(
			'api/oauth/',
			array(
				'controller' => 'api-oauth',
				'action'     => 'index'
				)		
			);		
		$router->addRoute('oauth-index', $route);

		$route = new Zend_Controller_Router_Route_Static(
			'api/oauth/request_token',
			array(
				'controller' => 'api-oauth',
				'action'     => 'request-token'
				)		
			);		
		$router->addRoute('oauth-request-token', $route);
		
		$route = new Zend_Controller_Router_Route_Static(
			'api/oauth/access_token',
			array(
				'controller' => 'api-oauth',
				'action'     => 'access-token'
				)		
			);
		$router->addRoute('oauth-access-token', $route);	
		
		$route = new Zend_Controller_Router_Route_Static(
			'api/oauth/authorize',
			array(
				'controller' => 'api-oauth',
				'action'     => 'authorize'
				)		
			);
		$router->addRoute('oauth-authorize', $route);

		// returns the router resource to bootstrap resource registry
		return $router;
	}
	
	/**
	 * Enter description here...
	 *
	 */
	protected function _initViewHelpers()
	{
		$this->bootstrap('layout');
		$layout = $this->getResource('layout');
		$view = $layout->getView();
		
		$view->doctype('XHTML1_STRICT');
		$view->headMeta()->appendHttpEquiv('Content-Type', 'text/html;charset=utf-8');
		$view->headTitle()->setSeparator(' - ');
		$view->headTitle('Zend Framework Tutorial');
		$view->addHelperPath(APPLICATION_PATH . '/views/helpers', 'Zend_View_Helper');
		
		return $view;
	}
}

?>